package com.alan.smzdm;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.commons.codec.binary.Base64;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class User2 {
	
	public static void user1Op() {
		String result = checkin1();
		JSONObject jsonObject = JSON.parseObject(result);
		String error_msg = jsonObject.getString("error_msg");
		String smzdm_id = jsonObject.getString("smzdm_id");
//		System.out.println(error_msg);
		JSONObject jsonObjectData = jsonObject.getJSONObject("data");
		String en_key = jsonObjectData.getString("en_key");
		String server_time = jsonObjectData.getString("server_time");
		//868447013010685
		String dataStr = "1102768378862154030730008_" + server_time;
		try {
			byte[] buf = DESUtil.encrypt(dataStr.getBytes(), en_key.getBytes());
			String str = Base64.encodeBase64String(buf);
			String d = URLEncoder.encode(str);
//			System.out.println("d=" + URLEncoder.encode(str));
			lottry1(d);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static String checkin1() {
		String reqData = "sk=YcmoAz7Dl7qLQogGPXI0X4qja0suv6mOBK8rKegdh3M%3D&token=NGE1MGF8MTQ5NTMzMzgyOXwxMTAyNzY4Mzc4fDNiM2YwYzY1Zjc4MjU2MTE2Mjg3M2M1N2Q3NzhiY2Y3&captcha=&f=android&s=ZVN9RrWhMAwBMLwqnSgamGPUy3NOrOF6&weixin=1&v=7.7&";
		URL url;
		try {
			url = new URL("https://api.smzdm.com/v1/user/checkin");
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");
			connection.setDoOutput(true);
			connection.setRequestProperty("User-agent", "smzdm_android_V7.7 rv:370 (Redmi 3S;Android6.0.1;zh)smzdmapp");
			connection.setRequestProperty("Cookie",
					"sess=NGE1MGF8MTQ5NTMzMzgyOXwxMTAyNzY4Mzc4fDNiM2YwYzY1Zjc4MjU2MTE2Mjg3M2M1N2Q3NzhiY2Y3;pid=862154030730008;partner_id=3;partner_id=3;device_id=63967b68ee9f10a88071d3bee3c772ec;imei=09b2344474dd37c29af6d5f31aa0ccf6;partner_name=smzdm_download;mac=02:00:00:00:00:00;smzdm_id=1102768378;login=1;device_push=1;network=wifi;device_smzdm_version=7.7;device_smzdm_version_code=370;device_s=ZVN9RrWhMAwBMLwqnSgamGPUy3NOrOF6;device_type=XiaomiRedmi 3S;device_system_version=6.0.1;device_smzdm=android;rs_id1=;rs_id2=;rs_id3=;rs_id4=;rs_id5=;smzdm_device=android;smzdm_user_source=ZVN9RrWhMAwBMLwqnSgamGPUy3NOrOF6;smzdm_version=7.7;");
			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			OutputStream out = connection.getOutputStream();
			out.write(reqData.getBytes());
			InputStream in = connection.getInputStream();

			BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
			String tmpStr = "";
			StringBuffer sb = new StringBuffer();
			while ((tmpStr = br.readLine()) != null) {
				sb.append(tmpStr);
				System.out.println(tmpStr);
			}
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void lottry1(String d) {
		URL url;
		String strD = d;// "3yOxc%2FlrYEZSrn8UuDDlVq%2OhWSamSBTD%2FupEJJ%2BT5tGxGB7giHs%2FfA%3D%3D";
		try {
			url = new URL("http://h5.smzdm.com/user/lottery/ajax_draw");
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");
			connection.setDoOutput(false);
			connection.setRequestProperty("User-Agent",
					"Mozilla/5.0 (Linux; Android 4.4.4; IUNI i1 Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36 {smzdm_android_V7.7 rv:370 (IUNI i1;Android4.4.4;zh)smzdmapp}");
			connection.setRequestProperty("Cookie",
					"partner_id=3; partner_name=smzdm_download; smzdm_id=1102768378; __ckguid=BjYNE5UONvaTA2VSs5pSA2; __jsluid=9379173904e88dc3e5c0e0d8764de95c; _ga=GA1.2.1491180802.1491445758; smzdm_wordpress_360d4e510beef4fe51293184b8908074=user%3A1102768378%7C1495333829%7C20f2ce6025b93d1cf770b99541100bcf; smzdm_wordpress_logged_in_360d4e510beef4fe51293184b8908074=user%3A1102768378%7C1495333829%7C479d890448e7383d9de15716e9adf374; user-role-smzdm=subscriber; sess=NGE1MGF8MTQ5NTMzMzgyOXwxMTAyNzY4Mzc4fDNiM2YwYzY1Zjc4MjU2MTE2Mjg3M2M1N2Q3NzhiY2Y3; device_id=ZVN9RrWhMAwBMLwqnSgamGPUy3NOrOF6; from=android; v=7.7; version=7.7; d="+strD+"; t=2da749374b1fdde8c9931d2a712308db; s=ZVN9RrWhMAwBMLwqnSgamGPUy3NOrOF6");
			InputStream in = connection.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
			String tmpStr = "";
			StringBuffer sb = new StringBuffer();
			while ((tmpStr = br.readLine()) != null) {
				sb.append(tmpStr);
				System.out.println(tmpStr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
